/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ddd.servlet;

import com.ddd.entity.Journal;
import com.ddd.service.JournalService;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class doJournal extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        String mode = request.getParameter("mode");

        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String fkPublisherID = request.getParameter("fkPublisherID");
        String frequency = request.getParameter("frequency");
        String foundYear = request.getParameter("foundYear");
        String languageSN = request.getParameter("languageSN");
        String ISSN = request.getParameter("ISSN");
        String embargo = request.getParameter("embargo");
        String embargoFreq = request.getParameter("embargoFreq");
        String oaLink = request.getParameter("oaLink");
        String picture = request.getParameter("picture");
        String beginVolume = request.getParameter("beginVolume");
        String beginIssue = request.getParameter("beginIssue");
        String authorized = request.getParameter("authorized");
        String currCommission = request.getParameter("currCommission");
        String pastCommission = request.getParameter("pastCommission");
        String fkContractID = request.getParameter("fkContractID");
        String note = request.getParameter("note");
        String cid = request.getParameter("cid");
        String ctime = request.getParameter("ctime");
        String uid = request.getParameter("uid");
        String utime = request.getParameter("utime");

        System.out.println(mode);
        System.out.println(id);
        System.out.println(name);
        System.out.println(fkPublisherID);
        System.out.println(frequency);
        System.out.println(foundYear);
        System.out.println(languageSN);
        System.out.println(ISSN);
        System.out.println(embargo);
        System.out.println(embargoFreq);
        System.out.println(oaLink);
        System.out.println(picture);
        System.out.println(beginVolume);
        System.out.println(beginIssue);
        System.out.println(authorized);
        System.out.println(currCommission);
        System.out.println(pastCommission);
        System.out.println(fkContractID);
        System.out.println(note);


        Journal journal = new Journal();
        journal.setId(id);
        journal.setName(name);
        journal.setFkPublisherId(fkPublisherID);
        journal.setFrequency(frequency);
        journal.setFoundYear(foundYear);
        journal.setLanguageSn(languageSN);
        journal.setIssn(ISSN);
        journal.setEmbargo(embargo);
        journal.setEmbargoFreq(embargoFreq);
        journal.setOaLink(oaLink);
        journal.setPicture(picture);
        journal.setBeginVolume(beginVolume);
        journal.setBeginIssue(beginIssue);
        journal.setAuthorized(authorized);
        journal.setCurrCommission(currCommission);
        journal.setPastCommission(pastCommission);
        journal.setFkContractId(fkContractID);
        journal.setNote(note);
        journal.setCid(cid);
        journal.setCtime(ctime);
        journal.setUid(uid);
        journal.setUtime(utime);

        JournalService service = new JournalService(request);
        if (mode.equals("add")) {
            service.save(journal);
            String message = URLEncoder.encode("新增成功！", "UTF-8");
            response.sendRedirect("BE/journal.jsp?mode=update&id=" + journal.getId() + "&message=" + message);
        } else if (mode.equals("update")) {
            service.update(journal);
            String message = URLEncoder.encode("更新成功！", "UTF-8");
            response.sendRedirect("BE/journal.jsp?mode=update&id=" + journal.getId() + "&message=" + message);
        } else if (mode.equals("delete")) {
            Journal target = service.load(id);
            service.delete(target);
            String message = URLEncoder.encode("刪除成功！", "UTF-8");
            response.sendRedirect("BE/journal.jsp?message=" + message);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
